<?php
/**
 * CK 开发框架
 * User: Clake
 * Date: 15/6/18
 * Time: 01:39
 */

namespace CK\Cache;


use CK\Util\Arr;

class Memcached extends Cache {
    /**
     * 相关实体类
     *
     * @var \Memcached
     */
    private $_cache;

    private $_conf = [
        'prefix'=>'ck_',
        'servers'=>[['host'=>'127.0.0.1','port'=>11211]]
    ];

    private $_prefix;

    private $_inied = false;

    public function __construct($conf=null) {
        $this->_conf = Arr::mrg($this->_conf,$conf);
        $this->_prefix = $this->_conf['prefix'];
        $this->_cache = new \Memcached();
    }

    public function initServer() {
        if ($this->_inied === true) {
            return;
        }

        if (isset($this->_conf['servers'])) {
            foreach ($this->_conf['servers'] as $host) {
                $this->connect($host['host'],$host['port']);
            }
        }
        $this->_inied = true;
    }

    public function connect($host,$port) {
        return $this->_cache->addServer($host,$port);
    }

    /**
     * 添加一个缓存项
     * @param string $key
     * @param string $value
     * @param int $expire
     * @return boolean
     */
    public function add($key,$value,$expire=300) {
        $this->initServer();
        return $this->_cache->add($this->_prefix.$key,$value,$expire);
    }

    public function set($key,$value,$expire=300) {
        $this->initServer();
        return $this->_cache->set($this->_prefix.$key,$value,$expire);
    }

    public function get($key) {
        $this->initServer();
        return $this->_cache->get($this->_prefix.$key);
    }

    public function exist($key) {
        $this->initServer();

        return $this->_cache->get($key)===false?false:true;
    }

    public function getServerList() {
        $this->initServer();
        $items = $this->_cache->getServerList();
        $servers = [];
        foreach ($items as $item) {
            $servers[] = ['k'=>$item['host'],'v'=>$item['host']];
        }
        return $servers;
    }


//    public function status($cmd,$stabid=null,$limit=null) {
//        if ($this->_cache_sw) {
//            if (!$this->_inied) {
//                $this->initServer();
//            }
//            if ($this->_type == self::CACHE_MEMCACHED) {
//                return $this->_cache->getServerList();
//            } else {
//                return $this->_cache->getExtendedStats($cmd,$stabid,$limit);
//            }
//        } else {
//            return false;
//        }
//    }

    public function getKeys() {
        $this->initServer();

        $items = $this->_cache->getAllKeys();
        $list = [];
        foreach ($items as $item) {
            $list[] = ['cache_key'=>$item];
        }
        return $list;
    }

    public function delete($key,$timeout=0) {
        $this->initServer();
        return $this->_cache->delete($this->_prefix.$key,$timeout);
    }

    public function flush() {
        $this->initServer();
        return $this->_cache->flush();
    }

    public function close() {
        if ($this->_inied) {
            $this->_cache->quit();
        }
    }
}